#include <bits/stdc++.h>
using namespace std;
#define int long long
#define F(i, a, b) for (int i = a; i <= b; i++)
#define R(i, a, b) for (int i = a; i >= b; i--)

#define mk(a, b) make_pair(a, b)
#define inv(x) ksm(x, Mod - 2)
#define mem(a, b) memset(a, b, sizeof a)

#define De(x) cerr << "------------------" << x << "--------------" << endl
#define Debug() cerr << "----------------------------------------" << endl
#define lson(x) x << 1
#define rson(x) x << 1 | 1
char buf[(1 << 21) + 5], *p1, *p2;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)

const int N = 2e5 + 5, TN = 1e6 + 5, Mod = 998244353, Mod1 = 147744151, inf = INT_MAX, Inf = LONG_LONG_MAX;
inline int ksm(int a, int b);
inline int read();
string s;
int T = 1, ans = 0, n, m, a[1005][1005], sum[1005][1005], P, x, y, xx, yy;

signed main() {
	freopen("tsuki.in", "r", stdin);
	freopen("tsuki.out", "w", stdout);
	n = read(), m = read(), P = read();
	F(i, 1, n)
	F(j, 1, m)
	a[i][j] = read();
	T = read();
	if (P == Mod) {
		F(i, 0, max(n, m)) sum[i][0] = sum[0][i] = 1;
		F(i, 1, n)
		F(j, 1, m)
		sum[i][j] = ((1ll * (sum[i - 1][j]%Mod * sum[i][j - 1] ) % Mod * inv(sum[i - 1][j - 1]) * a[i][j] % Mod)+Mod) % Mod;
		int tot = 1;
		while (T--) {
			x = read(), y = read(), xx = read(), yy = read();
			int Ans = (sum[xx][yy] *  (sum[x-1][y-1])%Mod* inv(sum[x - 1][yy]) % Mod * inv(sum[xx][y - 1]))%Mod ;
			Ans = (Ans + P)%P;	//	cout<<Ans<<endl;
			ans = ans + (tot ^ (Ans))%Mod1;
		   // ans = (ans + Mod1) % Mod1;
			tot++;
		}
		cout << (ans + Mod1) % Mod1;
		return 0;
	}
	int tot = 1;
	while (T--) {
		x = read(), y = read(), xx = read(), yy = read();
		int sum = 1;
		F(i, x, xx) {
			F(j, y, yy) {
				sum *= a[i][j];
				sum %= P;
			}
		}
		ans = ans + (tot ^ (sum % P));
		tot++;
	}
	cout << ans % 147744151;
	return 0;
}
inline int read() {
	int x = 0, f = 1;
	char c = getchar();
	while (c < '0' || c > '9') {
		if (c == '-')
			f *= -1;
		c = getchar();
	}
	while (c <= '9' && c >= '0') {
		x = (x << 3) + (x << 1) + (c ^ 48);
		c = getchar();
	}
	return x * f;
}
inline int ksm(int a, int b) {
	if (a == 0) return 1;
	if (b == 0) return 1;
	int aa = 1;
	while (b) {
		if (b & 1)
			aa *= a, aa %= Mod;
		a *= a;
		a %= Mod;
		b >>= 1;
	}
	return aa%Mod;
}
